#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct hun {
    int C;
    int D;
};

int maxh;

void dfs(int p, vector<vector<hun>>& mm, int C, int hurt) {
    if (p == 7) {
        if (C < 100) return;
        maxh = max(maxh, hurt);
        return;
    }

    if (C >= 100) {
        for (int i = p; i < mm.size(); i++) {
            int aaa = 0;
            for (int j = 0; j < mm[i].size(); j++) {
                aaa = max(aaa, mm[i][j].D);
            }
            hurt += aaa;
        }
        dfs(7, mm, 100, hurt);
    } else {
        for (int i = 0; i < mm[p].size(); i++) {
            dfs(p + 1, mm, C + mm[p][i].C, hurt + mm[p][i].D);
        }
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        maxh = -1;
        vector<vector<hun>> mm(8);
        for (int i = 0; i < n; i++) {
            int P, C, D;
            cin >> P >> C >> D;
            hun h = {C, D};
            mm[P].push_back(h);
        }
        for (int i = 1; i <= 6; i++)
            sort(mm[i].begin(), mm[i].end(), [](hun h1, hun h2) {
                if (h1.C != h2.C) return h1.C > h2.C;
                return h1.D > h2.D;
            });
        dfs(1, mm, 0, 0);
        if (maxh == -1)
            cout << "TAT" << endl;
        else
            cout << maxh << endl;
    }
    return 0;
}